package cn.edu.scau.cmi.limingben.hibernateprog.control;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

import cn.edu.scau.cmi.limingben.hibernateprog.model.dao.StudentDAO;
import cn.edu.scau.cmi.limingben.hibernateprog.model.dao.TeacherDAO;
import cn.edu.scau.cmi.limingben.hibernateprog.model.domain.Student;
import cn.edu.scau.cmi.limingben.hibernateprog.model.domain.Teacher;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;

public class HibernateProgController implements Initializable {
    @FXML
    private ListView<Student> studentList;
    @FXML
    private ListView<Student> teachList;
    @FXML
    private ListView<Teacher> teacherList;
    @FXML
    private ListView<Teacher> tutorList;
    @FXML
    private TextField addStudentName;
    @FXML
    private TextField changeStudentName;
    @FXML
    private TextField addTeacherName;
    @FXML
    private TextField changeTeacherName;
    private StudentDAO studentDAO;
    private TeacherDAO teacherDAO;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        studentDAO = new StudentDAO();
        teacherDAO = new TeacherDAO();
        updateList();
        studentList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Student>() {
            @Override
            public void changed(ObservableValue<? extends Student> observable, Student oldValue, Student newValue) {
                updateTutor(newValue);
            }
        });
        teacherList.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Teacher>() {
            @Override
            public void changed(ObservableValue<? extends Teacher> observable, Teacher oldValue, Teacher newValue) {
                updateTeach(newValue);
            }
        });
    }

    private void updateTutor(Student student) {
        if (student != null) {
            for(Student s: studentDAO.listAll()){
                if(student.getId() == s.getId()) {
                    student = s;
                }
            }
            tutorList.setItems(FXCollections.observableArrayList(student.getTeacher()));
        } else {
            tutorList.setItems(FXCollections.observableArrayList());
        }
    }

    private void updateTeach(Teacher teacher) {
        if (teacher != null) {
            List<Student> students =  studentDAO.listAll();
            teachList.setItems(FXCollections.observableArrayList(students.stream().filter((s)-> s.getTeacher() != null && s.getTeacher().getId() == teacher.getId()).collect(Collectors.toList())));
        } else {
            teachList.setItems(FXCollections.observableArrayList());
        }
    }

    private Student getSelectStudent(ListView<Student> listView) {
        return listView.getSelectionModel().getSelectedItem();
    }

    private Teacher getSelectTeacher(ListView<Teacher> listView) {
        return listView.getSelectionModel().getSelectedItem();
    }

    private void updateList() {
        studentList.setItems(FXCollections.observableArrayList(studentDAO.listAll()));
        teacherList.setItems(FXCollections.observableArrayList(teacherDAO.listAll()));
    }

    @FXML
    public void addStudent(ActionEvent e) {
        studentDAO.add(addStudentName.getText());
        updateList();
    }

    @FXML
    public void removeStudent(ActionEvent e) {
        Student student = studentList.getSelectionModel().getSelectedItem();
        studentDAO.remove(student);
        updateList();
    }

    @FXML
    public void changeStudent(ActionEvent e) {
        Student student = studentList.getSelectionModel().getSelectedItem();
        studentDAO.setName(student, changeStudentName.getText());
        updateList();
    }

    @FXML
    public void setTutor(ActionEvent e) {
        Student student = getSelectStudent(studentList);
        Teacher teacher = getSelectTeacher(teacherList);
        if (student == null) {
            return;
        }
        student.setTeacher(teacher);
        studentDAO.saveOrUpdate(student);
        updateTutor(student);
        if(teacher != null){
            updateTeach(teacher);
        }
    }
    @FXML
    public void addTeacher(ActionEvent e) {
        teacherDAO.add(addTeacherName.getText());
        updateList();
    }

    @FXML
    public void removeTeacher(ActionEvent e) {
        Teacher teacher = teacherList.getSelectionModel().getSelectedItem();
        teacherDAO.remove(teacher);
        updateList();
    }

    @FXML
    public void changeTeacher(ActionEvent e) {
        Teacher teacher = teacherList.getSelectionModel().getSelectedItem();
        teacherDAO.setName(teacher, changeTeacherName.getText());
        updateList();
    }
    @FXML
    public void removeTeach(ActionEvent e){
        Student student = getSelectStudent(teachList);
        if(student == null){
            return;
        }
        student.setTeacher(null);
        studentDAO.saveOrUpdate(student);
        updateTeach(getSelectTeacher(teacherList));
        updateTutor(getSelectStudent(studentList));
    }

}
